package com.fh.web.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.bo.PageFilterBo;
import com.fh.dto.PubSystem;
import com.fh.dto.SysUser;
import com.fh.web.constants.FhManagerConstant;
import com.fh.web.constants.GlobalConstants;
import com.fh.web.service.UserMangerService;
import com.fh.web.vo.ShowGridVo;
import com.fh.web.vo.SysUserVo;

/**
 * 用户管理controller
 * @author Administrator
 *
 */
@Controller
@RequestMapping("userManger")
public class UserMangerController {
	
	// 打印logger日志
		private final Logger LOGGER = LoggerFactory.getLogger(getClass());
		
		 @Resource
		 private UserMangerService userMangerService;
	
	/**
	 * 跳转用户管理页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/userListManager", method = { RequestMethod.GET, RequestMethod.POST })
	private ModelAndView userListManager(HttpServletRequest request, HttpServletResponse response) {
		// 申明前台返回参数map
		Map<String, Object> resMap = new HashMap<String, Object>();
		// 获取前台请求参数
		String pId = request.getParameter("id");
		try {
			// 从session获取用户登录信息
			SysUser user = (SysUser) request.getSession().getAttribute(GlobalConstants.SYS_USER);
			SysUserVo sysUserVo=new SysUserVo();
			BeanUtils.copyProperties(user, sysUserVo);
			if (user != null) {
				// 获取用户权限
				resMap = userMangerService.userCanOperation(sysUserVo, pId);
				resMap.put("resCode", FhManagerConstant.RST_SUCCESS);
			} else {
				resMap.put("resCode", FhManagerConstant.RST_FAIL);
			}
		} catch (Exception e) {
			LOGGER.error("跳转用户管理页面", e);
			resMap.put("resCode", FhManagerConstant.RST_FAIL);
		}
		return new ModelAndView("admin/user", resMap);
	}
	
	/**
	   * 分页查询用户信息
	   * 
	   * @param ph
	   * @param request
	   * @return
	   */
	  @ResponseBody
	  @RequestMapping(value = "/staffListDate", method = RequestMethod.POST)
	  private ShowGridVo staffListDate(PageFilterBo ph, HttpServletRequest request) {
	    // 申明easyUi前台封装数据
	    ShowGridVo gridBo = new ShowGridVo();
	    try {
	      // 分页查询区域下所有的用户
	      Map<String,Object> map = userMangerService.selectAllUser(ph, request.getParameter("sysCode"),request.getParameter("userName"));
	      Long toltal = (Long) map.get("pageTotal");
	      // 设置分页数据
	      List<SysUser>  list = (List<SysUser>) map.get("list");
	      gridBo.setRows(list);
	      if (list != null && list.size() > 0) {
	        // 设置总记录数 
	        gridBo.setTotal(toltal.intValue());
	      } else {
	        gridBo.setTotal(0);
	      }
	    } catch (Exception e) {
	      LOGGER.error("获取用户信息失败", e);
	      return null;
	    }
	    return gridBo;
	  }
	  
	  /**
		 * 编译用户
		 * 
		 * @param request
		 * @param id
		 * @return
		 */
		@RequestMapping(value = "/edit", method = RequestMethod.GET)
		private ModelAndView edit(HttpServletRequest request, Long id) {
			try {
				// 编译用户
				request.setAttribute("userBo", userMangerService.editUser(id));
			} catch (Exception e) {
				LOGGER.error("编译用户信息异常", e);
			}
			return new ModelAndView("admin/userEdit");
		}
		

		/**
		 * 更新用户信息 
		 * 
		 * @param request
		 * @param userBean
		 * @return
		 * @throws Exception
		 */
		@ResponseBody
		@RequestMapping(value = "/updateUser", method = RequestMethod.POST)
		private void updateUser(HttpServletRequest request, SysUserVo userBean,HttpServletResponse response) throws Exception {
			// 申明easyUi前台封装数据
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			try {
				// 从session获取用户数据
				SysUser sessionUserDto = (SysUser) request.getSession().getAttribute(GlobalConstants.SYS_USER);
				if (sessionUserDto == null) {
					out.print("用户没登录");
				}
				// 更新用户信息
				Integer createUserId = sessionUserDto.getUserId().intValue();// 获取创建人id
				userBean.setNotes(new String(userBean.getNotes().getBytes("iso8859-1"),"utf-8"));
				int result = userMangerService.updateUserById(userBean, createUserId);
				if (result == 1) {
					out.print("修改成功");
				}
			} catch (Exception e) {
				out.print("修改失败");
				LOGGER.error("更新用户信息异常", e);
			}
		}
		/**
		 * 获取pub_system的信息
		 * 
		 * @param request
		 * @param userBean
		 * @return
		 * @throws Exception
		 */
		@ResponseBody
		@RequestMapping(value = "/getSysCode", method = RequestMethod.POST)
		private List<PubSystem> getSysCode(HttpServletRequest request,HttpServletResponse response) throws Exception {
			List<PubSystem> pubSystem = userMangerService.getSysCode();
			return pubSystem;
			
		}
		
		/**
		 * 删除用户
		 * 
		 * @param request
		 * @param id
		 * @return
		 * @throws Exception 
		 */
		@ResponseBody
		@RequestMapping(value = "/delete", method = { RequestMethod.GET,RequestMethod.POST })
		private int delete(HttpServletRequest request, HttpServletResponse response,Long id) throws Exception {
		  return userMangerService.deleteUserById(id);
			
		}
		
		/**
		 * 编译用户
		 * 
		 * @param request
		 * @param id
		 * @return
		 * @throws Exception 
		 */
		@ResponseBody
		@RequestMapping(value = "/updateFun", method = { RequestMethod.GET,RequestMethod.POST })
		private int updateFun(HttpServletRequest request, HttpServletResponse response, Long id) throws Exception {
			return userMangerService.updateUserById(id);
		}
		
}
